<?php

namespace app\admins\controller;

use app\admins\model\Links as linksModel;

class Links extends Base
{
    use Curd;

    // 初始化
    protected function initialize()
    {
        parent::initialize();
        $this->model = new linksModel();
        $this->sort = ['id' => 'desc'];

        $this->rule = [
            'name|网站名称' => 'require|chsDash|token|unique:links', //验证token且值唯一
            'url|网站地址' => 'require|url|unique:links', //验证值唯一
            'sort|排序' => 'require|integer|max:4',
            'status|状态' => 'require|in:0,1',
        ];

        if ($this->action === 'save') {
            $this->param['time'] = time();
        }
    }

    // 数据返回
    private function datas($datas)
    {
        $datas = $datas->map(function ($item) {
            $item['url'] = '<a href="' . $item['url'] . '" title="' . $item['name'] . '" target="_blank">' . $item['url'] . '</a>';
            $item['logo'] = $item['logo'] ? '<img src="' . $item['logo'] . '" style="width:50%;height:100%" />' : '';
            $item['time'] = date('Y-m-d H:i:s', $item['time']);
            return $item;
        });
        return $datas;
    }
}
